Skip to content

fix(memory): add reindex recursion and API write L1 vectorization - #3679

Open
Franch-Toast wants to merge 6 commits into
volcengine:mainfrom
Franch-Toast:fix/memory-l1-reindex-recursive
Open

Franch-Toast wants to merge 6 commits into
volcengine:mainfrom
Franch-Toast:fix/memory-l1-reindex-recursive

Conversation

@Franch-Toast

@Franch-Toast Franch-Toast commented Aug 2, 2026 •

Copy link
Copy Markdown

fix(memory): add reindex recursion and API write L1 vectorization

Description

Extends #3665 with two additional fixes for the memory write pipeline L1 vector gap.

PR #3665 resolved the SessionCommit path (reorder overview generation before vectorization, emit L1 per parent directory), but left two gaps that this PR addresses:

  1. Reindex recursion (_process_memory_directory): The method previously ignored msg.recursive, meaning ov reindex on a memory root (e.g. entities/) only processed files in the immediate directory and never descended into category subdirectories (e.g. entities/projects/). Now, when recursive=True, subdirectories are processed recursively with per-child error isolation.

  2. API write path L1 vectorization (_write_memory_with_refresh): Writing a memory file via the API (e.g. ov write entities/projects/illuminator.md) generated .overview.md for the memory type root but never vectorized the direct parent directory overview as L1. Thinking-mode retrieval (level=[0,1]) could not discover the category directory until a manual reindex. Now the direct parent's overview is refreshed and vectorized as L1 (zero LLM cost, template-rendered only).

Background — Issue Chain

Issue Description
#422 Initial report: memory search returns empty after session commit
#505 Identified O(n²) token cost of LLM-based memory refresh → memory path deliberately skips SemanticQueue
#3640 SessionCommit writes leaf files + .overview.md but never enqueues L1 vectors
#2797 ov reindex on memory directories does not recurse into subdirectories
#3665 (PR) Fixed SessionCommit path only

Design Decisions

Decision Rationale
Only direct parent, not ancestor chain HierarchicalRetriever's global search (level=[0,1], no depth limit) discovers any L1 directory regardless of depth; ancestor L1 records are created during initialization/reindex
No LLM / SemanticQueue for memory writes Avoids O(n²) token cost (Issue #505); template-rendered overview is sufficient for L1 discovery
lock=None for recursive children Prevents pathlock self-deadlock when parent holds a lock and child tries to acquire a nested lock (Issue #1531)
skip_vectorization propagated to children Reindex Step 1 (structure scan) sets skip_vectorization=True; children must inherit this to avoid premature embedding

Human Involvement

  • A human participated in the implementation or review loop
  • This PR was generated entirely by AI agents without human participation in the loop

Related Issue

Relates #3640.
Fixes #2797.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)
  • Performance improvement
  • Test update

Changes Made

  • openviking/storage/queuefs/semantic_processor.py — _process_memory_directory now collects subdirectories alongside files; when msg.recursive=True, recursively invokes itself for each non-hidden child directory with lock=None and per-child error isolation (child failure logged as warning, does not block parent). Write-path behavior unchanged (recursive=False skips the new block entirely).
  • openviking/storage/content_write.py — _write_memory_with_refresh now determines the file's direct parent via VikingURI(uri).parent; if parent differs from root_uri, refreshes the parent's overview and calls vectorize_directory_meta(include_overview=True, include_abstract=False) to create the L1 record. Failure is non-blocking (try/except with warning log). semantic_status changed from "skipped" to "partial" (L1 present, no L0).
  • tests/unit/storage/test_memory_reindex_recursive.py — 5 new tests covering recursive behavior.
  • tests/unit/storage/test_content_write_memory_l1.py — 5 new tests covering API write L1 vectorization.

Leaf/level invariants preserved: leaf memory files continue through the standard ContextLevel.DETAIL (L2) flow; each parent directory receives exactly one ContextLevel.OVERVIEW (L1) record with is_leaf=False, context_type="memory", and the directory URI; no L0 ABSTRACT record is emitted for memory overviews.

Testing

  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have tested this on the following platforms:
    • Linux
    • macOS
    • Windows

New tests (10 total):

File Tests Coverage
tests/unit/storage/test_memory_reindex_recursive.py 5 recursive=True processes subdirs; recursive=False skips; skip_vectorization propagated; hidden dirs skipped; child failure isolated
tests/unit/storage/test_content_write_memory_l1.py 5 nested path vectorizes parent L1; shallow path no redundant refresh; empty overview skips vectorization; vectorization failure non-blocking; semantic_status="partial"

Regression: All 43 pre-existing tests from PR #3665 (test_issue_3640_parent_vectors.py + test_vectorize_file_strategy.py) pass unchanged. Total: 53 tests pass, 0 failures.

Lint: ruff check and ruff format clean on all modified files.

Checklist

  • My code follows the project's coding style
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

Screenshots (if applicable)

N/A — backend logic changes with no UI impact.

Additional Notes

This PR is built on top of PR #3665 (cherry-picked commits d513c6a9 and e33ff176). The additional commit 1f9df5fd contains the new fixes and tests.

Risk Assessment

Risk Mitigation
Reindex recursion depth unbounded Memory directories are max 4 levels deep (events/year/month/day); practical recursion is bounded
Child failure blocks parent processing Each child wrapped in try/except; failure logged as warning, parent continues
Extra I/O on API write path (read overview + vectorize) Only when overview is non-empty; failure non-blocking; no LLM call involved
Regression on PR #3665 assertions All 43 original tests re-run and pass

huangruiteng and others added 3 commits August 1, 2026 15:17
SessionCommit could write memory leaf files and their .overview.md
without enqueuing the L1 directory vectors, leaving parent
directories without vector records. Hierarchical retrieval then
returned zero results until a vectors_only reindex rebuilt the
subtree. Default memory files (identity.md, soul.md) written by
initialize_memory_files were likewise never embedded.

Two root causes:

1. generate_overview() wrote .overview.md after _vectorize_memories()
   ran, so the new overview file was never vectorized.
2. initialize_memory_files() wrote defaults directly through
   viking_fs.write_file() with no embedding enqueue.

Fixes:

- Reorder apply_operations so overview generation happens before
  _vectorize_memories; collect the written overview URIs and pass
  them as a new overview_uris parameter.
- _vectorize_memories enqueues one L1 OVERVIEW record per overview
  URI via vectorize_directory_meta(include_abstract=False), reusing
  the shared owner/timestamp/telemetry path. No L0 abstract record
  is emitted because no .abstract.md was written; overview files are
  kept out of written_uris so the leaf flow is unchanged.
- vectorize_directory_meta gains include_abstract (default True) so
  callers that only wrote .overview.md can skip the L0 record.
- initialize_memory_files returns the list of URIs it newly created;
  pre-existing files are skipped and not returned.
- Add MemoryUpdater.initialize_and_vectorize_default_files and use
  it from compressor v2/v3 so newly initialized defaults are embedded
  on first session. Pre-existing defaults are not re-embedded.

Tests:
- tests/unit/session/memory/test_issue_3640_parent_vectors.py covers
  initialize_memory_files return semantics, the shared v2/v3 helper
  (success / no-op / vikingdb-unavailable), and apply_operations
  emitting exactly one L1 OVERVIEW per parent while keeping the leaf
  at L2 DETAIL and .overview.md out of written_uris.
- tests/unit/test_vectorize_file_strategy.py covers
  include_abstract=False producing exactly one OVERVIEW record and
  the default still producing L0+L1.

Fixes volcengine#3640
Ruff lint fix + format over the changed test file; no behavior change.
Extends PR volcengine#3665 with two additional fixes:

1. _process_memory_directory now honors msg.recursive flag, processing
   subdirectories recursively during reindex. Write-path behavior is
   unchanged (recursive=False).

2. ContentWriteCoordinator._write_memory_with_refresh now vectorizes
   the direct parent directory overview as L1 after
   refresh_schema_overview, aligning API writes with SessionCommit path.

Fixes volcengine#2797 (reindex recursion)
Relates volcengine#3640 (API write path)
@Franch-Toast

Copy link
Copy Markdown
Author

@huangruiteng
Hello! I have identified the same issue as you, and based on your commit, I have added a fix for the API write issue. You are welcome to take a look and provide feedback!

@Franch-Toast
Franch-Toast marked this pull request as draft August 3, 2026 02:24
@Franch-Toast
Franch-Toast marked this pull request as ready for review August 3, 2026 02:29
@Franch-Toast

Copy link
Copy Markdown
Author

The conflict caused by the v0.4.12 update has been resolved.
I think there might be an error in openviking/utils/embedding_utils.py in v0.4.12. It should be vectorized L1 by default, and explicitly specified to vectorize L0, but in main it's the opposite (vectorized L0 by default, and explicitly specified to vectorize L1). I corrected this in commit 4f0c77a.

@Franch-Toast

Copy link
Copy Markdown
Author

@qin-ctx Hello! Could you please take a moment to review this? Thank you!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

Semantic reindex leaves missing .abstract.md/.overview.md, fails parent directory rollups, and Rust CLI times out while server continues processing

2 participants